﻿using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace Nantianmen.EntityFrameworkCore.ValueConveters
{
    public class TransformsValueConverter : ValueConverter<List<Dictionary<string, string>>?, string?>
    {
        private const JsonSerializerOptions? nullOptions = null;

        public TransformsValueConverter() : this(
            value => value == null ? null : JsonSerializer.Serialize(value, nullOptions),
            text => string.IsNullOrWhiteSpace(text) ? null : JsonSerializer.Deserialize<List<Dictionary<string, string>>?>(text, nullOptions))
        {

        }

        public TransformsValueConverter(
           Expression<Func<List<Dictionary<string, string>>?, string?>> convertToProviderExpression,
           Expression<Func<string?, List<Dictionary<string, string>>?>> convertFromProviderExpression,
           ConverterMappingHints? mappingHints = null) : base(convertToProviderExpression, convertFromProviderExpression, mappingHints)
        {
        }
    }
}
